package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.marketBean.MarketFeedback;
import com.cskaoyan.bean.marketBean.MarketUser;
import com.cskaoyan.service.wx.WxFeedBackService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 意见反馈模块
 *
 * @author 曾添乐啦
 * @since 2023/01/01 21:56
 */
@RestController
@RequestMapping("wx/feedback")
public class WxFeedBackController {

    @Autowired
    WxFeedBackService wxFeedBackService;

    /**
     * post请求提交的参数，用MarketFeedback对象来接收
     * 请求提交的JSON参数如下
     * content: "好吃又实惠"
     * feedType: "商品相关"
     * hasPicture: true
     * mobile: "12345678901"
     * picUrls: ["http://182.92.235.201:8083/wx/storage/fetch/udlyqkyh19qmfcmghqhc.png"]
     *
     * 后台意见反馈模块显示的数据如下
     * addTime: "2023-01-02 00:32:22"
     * content: "好吃又实惠"               √
     * deleted: false
     * feedType: "商品相关"                 √
     * hasPicture: true                    √
     * id: 47
     * mobile: "12345678901"                √
     * picUrls: ["http://182.92.235.201:8083/wx/storage/fetch/udlyqkyh19qmfcmghqhc.png"]   √
     * status: 1
     * updateTime: "2023-01-02 00:32:22"
     * userId: 1
     * username: "user123"
     *
     * ==========
     * 也就是说返回数据需要额外在添加
     * addTime
     * updateTime
     * deleted
     * id
     * status
     * userId
     * username
     * 用户id和用户名直接从session域中获取
     * 其他几个数据交个service层封装
     *      addTime
     *      updateTime
     *      deleted      有默认值（可以不处理）
     *      id          有默认值
     *      status     有默认值
     * ========================
     * 后台实际回显的数据只有七个
     *
     *   `id` int NOT NULL AUTO_INCREMENT,
     *   `user_id` int NOT NULL DEFAULT '0' COMMENT '用户表的用户ID',
     *   `username` varchar(63) NOT NULL DEFAULT '' COMMENT '用户名称',                      √
     *   `mobile` varchar(20) NOT NULL DEFAULT '' COMMENT '手机号',                           √
     *   `feed_type` varchar(63) NOT NULL DEFAULT '' COMMENT '反馈类型',                       √
     *   `content` varchar(1023) NOT NULL COMMENT '反馈内容',                                  √
     *   `status` int NOT NULL DEFAULT '0' COMMENT '状态',                              默认
     *   `has_picture` tinyint(1) DEFAULT '0' COMMENT '是否含有图片',                   默认
     *   `pic_urls` varchar(1023) DEFAULT NULL COMMENT '图片地址列表，采用JSON数组格式',
     *   `add_time` datetime DEFAULT NULL COMMENT '创建时间',                                 √
     *   `update_time` datetime DEFAULT NULL COMMENT '更新时间',
     *   `deleted` tinyint(1) DEFAULT '0' COMMENT '逻辑删除',                          默认
     * @return
     */
    //@RequiresPermissions("*")
    @RequestMapping("submit")
    public BaseRespVo insertFeedBack(@RequestBody MarketFeedback marketFeedback) {

        //从session域拿去用户信息
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipal();
        Integer userId = marketUser.getId();
        String username = marketUser.getUsername();
        marketFeedback.setUserId(userId);
        marketFeedback.setUsername(username);
        int insertAffected = wxFeedBackService.insertFeedBack(marketFeedback);
        if (insertAffected==1){
            return BaseRespVo.ok("success");
        }
        return BaseRespVo.invalidParameter("反馈意见失败，工作人员繁忙请稍后在反馈");
    }

}
